<template>
  <div id="main-body" class="bg-primary-100 flex justify-center h-screen">
    <section class="container flex justify-center items-center rounded ">
      <div class="hidden md:block">
        <img src="../assets/images/bg.png" alt="" class="rounded-l max-h-[500px]">
      </div>
      <div class="rounded-r w-80 h-[500px] my-8 px-2 py-4 bg-white shadow shadow-gray-300">
        <div class="text-center text-lg py-6">用户注册</div>
        <el-form 
          ref="formRef" 
          :model="form" 
          :rules="rules" 
          label-width="0px" 
          class="px-4"
          size="large"
        >
          <el-form-item prop="username">
            <el-input 
              v-model="form.username" 
              placeholder="请输入用户名" 
              :prefix-icon="User"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="请输入密码" 
              :prefix-icon="Lock"
            />
          </el-form-item>

          <el-form-item>
            <el-button 
              type="success" 
              class="w-full" 
              @click="submitForm"
            >
              注册
            </el-button>
          </el-form-item>

          <div class="text-center text-sm my-2">
            已有账号
            <a class="text-blue-500" href="/login">去登录</a>
          </div>
        </el-form>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { User, Message, Lock, Checked } from '@element-plus/icons-vue'
import registerApi from '@/api/user'

// Element Plus form validation
import { ElMessage } from 'element-plus'

defineOptions({
  name: 'Register',
})

const formRef = ref()

const form = reactive({
  username: '',
  password: '',
  vcode: ''
})

const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 3, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ],
  re_password: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次密码输入不一致'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ]
}

const refreshVcode = () => {
  // 验证码刷新逻辑
  console.log('刷新验证码')
}

const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      // 表单验证通过，提交数据
      const formData = {
        username: form.username,
        email: form.email,
        password: form.password,
        re_password: form.re_password
      }
      
      registerApi.register(formData).then(response => {
        ElMessage.success('注册成功')
        // 可以在这里添加跳转逻辑
        // router.push('/login')
      }).catch(error => {
        if (error.response.data.detail) {
          for (const key in error.response.data.detail) {
            ElMessage.error(error.response.data.detail[key])
          }
        } else {
          ElMessage.error('注册失败: ' + (error.response?.data?.detail || '未知错误'))
        }
      })
    } else {
      ElMessage.error('请正确填写表单信息')
      return false
    }
  })
}
</script>
